//初始页面用的商品数据
var initGoodsDB = require('../data/goodsDB').goodsDB;

//用户添加商品到购物车中的数据
var goodsInCarDB = [];
//打印在终端窗口加入购物车的的商品图片
var output_imgInCar = "";

const express = require('express');
const app = express();
var i = 1;
app.get("/jumeiyoupin/homepage", (request, response) => {

    //允许该请求跨域
    response.setHeader("Access-Control-Allow-Origin", "*");
    console.log("调用服务器的hello方法" + (i++) + request.query.inite_homepage);

    //将加入购物车的商品添加到服务器上
    if (request.query.add_shoppingcar) {
        console.log("把" + request.query.add_shoppingcar + "图片加入购物车");
        for (var i = 1; i < initGoodsDB.length; i++) {
            if (request.query.add_shoppingcar == initGoodsDB[i].img) {
                goodsInCarDB.push(initGoodsDB[i]);
                output_imgInCar += initGoodsDB[i].img + ","
            }
        }
        console.log("购物车中：" + output_imgInCar);
    }

    //将数据响应给homepage.html
    if (request.query.inite_homepage) {
        response.send(initGoodsDB);
    }

    //将数据响应给shoppingcar.html
    if (request.query.inite_shoppingcar) {
        response.send(goodsInCarDB);
    }
    console.log("购物车中：" + goodsInCarDB);
})
const port = 8400;
app.listen(8400, () => {
    console.log("web应用服务器启动成功,端口号：" + port);
})